package com.yzt.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.base.dto.ZTreeDTO;
import com.yzt.base.kit.BaseResult;
import com.yzt.base.utils.RequestUtil;
import com.yzt.sys.entity.Permission;
import com.yzt.sys.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author Y
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/sys/permission")
@Api(tags = "权限相关接口")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("list")
    @ApiOperation("权限列表")
    public BaseResult<PageInfo<Permission>> list(HttpServletRequest request, int page, int limit) {
        Map<String, Object> parameterMap = RequestUtil.getParameterMap(request);
        PageInfo<Permission> pageInfo = PageHelper.startPage(page, limit).doSelectPageInfo(() -> permissionService.list(Wrappers.<Permission>query().allEq(parameterMap)));
        return BaseResult.okData(pageInfo);
    }

    @GetMapping("ztreeList")
    @ApiOperation("ztree权限列表")
    public BaseResult<List<ZTreeDTO>> ztreeList(HttpServletRequest request) {
        return permissionService.getPermissionZTreeData();
    }

    @PostMapping("")
    @ApiOperation("保存权限信息")
    public BaseResult save(Permission p) {
        boolean n = permissionService.saveOrUpdate(p);
        if (n) {
            return BaseResult.ok();
        }
        return BaseResult.fail();
    }

    @PutMapping("")
    @ApiOperation("修改权限信息")
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "query")
    public BaseResult update(Permission p) {
        boolean n = permissionService.saveOrUpdate(p);
        if (n) {
            return BaseResult.ok();
        }
        return BaseResult.fail();
    }

}
